from fastapi import APIRouter, Depends, HTTPException, Request, status
from app.utils.jwt import create_token, check_token
from pydantic import BaseModel
from app.utils.oauth import oauth2_scheme
from typing import Union
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from app.utils.oauth import authenticate_user, fake_users_db, get_current_active_user
from app.tasks.test_model.tasks import test_task

from app.sqlModels.test_db import TestModel
from app.sqlModels import session

# 令牌模型
class Token(BaseModel):
    access_token: str
    token_type: str

class TokenData(BaseModel):
    username: str | None = None



class User(BaseModel):
    username: str
    email: Union[str, None] = None
    full_name: Union[str, None] = None
    disabled: Union[bool, None] = None


router = APIRouter(
    prefix="/items",
    tags=["items"],
    responses={404: {"description": "Not found"}},
    #dependencies=[Depends(check_token)]

)

@router.get("/{item_id}")
async def get_items(item_id: int):
    test_task.delay()

    return {"item_id": item_id}


@router.get("/")
async def get_items(current_user: User = Depends(get_current_active_user)):
    #print(Request.headers.user_id)
    return {"item_id": [1, 2, 3, 4]}




@router.post("/get-token", response_model=Token)
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):

    user = authenticate_user(fake_users_db, form_data.username, form_data.password)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )
    access_token = create_token(payload={"username": user.username})
    return {"access_token": access_token, "token_type": "bearer"}


@router.post("/create-data")
async def post_create_data():
    user1 = TestModel(name="闫子亮", email="17709502955@qq.com", age=12, )
    user2 = TestModel(name="马婧", email="3243423@qq.com", age=43, )
    user3 = TestModel(name="yanzil", email="3422413@qq.com", age=66, )

    session.add(user1)
    session.add(user2)
    session.add(user3)
    session.commit()
    session.close()
    return 201
    pass

